/*
 * Copyright (C) 2009 Show SMS open source project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.bydavy.util.showsms;

//~--- non-JDK imports --------------------------------------------------------

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;

import android.content.Context;
import android.content.Intent;

import android.net.Uri;

import com.bydavy.android.showsms.R;
import com.bydavy.model.showsms.SSMessage;

/**
 * Class to manage Notifications
 * Used to display Notifications on SMS reception
 *
 *
 * @version        1.0, 09/11/17
 * @author         Davy L.
 */
public class NotificationEasier {
    //~--- static fields ------------------------------------------------------

    /** Field description */
    private static final int NOTIF_NEW_MESSAGE_ID = 1;

    /** Field description */
    public static boolean vibrate = false;

    /** Field description */
    private static NotificationManager notificationManager;

    /** Field description */
    public static String soundUri;

    //~--- methods ------------------------------------------------------------

    /**
     * Must be called before all call to a static method of NotificationEasier class
     *
     * @param notifSoundUri Sound URI for notifications
     * @param notifVibrate Vibrate on notifications (true for vibration)
     * @param context Application context
     */
    public static void init(String notifSoundUri, boolean notifVibrate, Context context) {
        Log.v("NotificationEasier: init()");
        soundUri = notifSoundUri;
        vibrate  = notifVibrate;

        // Get the notification Manager
        String svcName = Context.NOTIFICATION_SERVICE;

        notificationManager = (NotificationManager) context.getSystemService(svcName);
    }

    /**
     * Called for display a notification with SMS informations
     *
     * @param aMessage the message to display in notifications
     * @param context application context
     */
    public static void NewMessage(SSMessage aMessage, Context context) {
        Log.v("NotificationEasier: NewMessage()");

        Notification notification;

        notification = createNotification(aMessage, context);
        notificationManager.notify(NOTIF_NEW_MESSAGE_ID, notification);
    }

    /**
     * Cancel new message notifications
     *
     */
    public static void cancelNewMessage() {
        notificationManager.cancel(NOTIF_NEW_MESSAGE_ID);
    }

    /**
     * Called to create a Notification
     *
     * @param aMessage the message to display in notifications
     * @param context application context
     *
     * @return
     */
    private static Notification createNotification(SSMessage aMessage, Context context) {

        // Notification infos
        int          icon         = R.drawable.show_sms_icon;
        long         when         = System.currentTimeMillis();
        CharSequence contentTitle = aMessage.getPerson().getNameOrPhoneNumber();
        CharSequence contentText  = aMessage.getBody();
        CharSequence tickerText   = contentTitle + " - " + contentText;

        // Notification intent on open
        Intent notificationIntent = new Intent();
        long   threadId           = aMessage.getThreadId();

        notificationIntent.setAction(Intent.ACTION_VIEW);
        notificationIntent.addCategory(Intent.CATEGORY_DEFAULT);
        notificationIntent.setData(Uri.withAppendedPath(SSMessage.THREAD_ID_CONTENT_URI, String.valueOf(threadId)));
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

        // the next two lines initialize the Notification, using the configurations above
        Notification notification = new Notification(icon, tickerText, when);

        if (vibrate) {
            Log.v("NotificationEasier: phone vibrating");
            notification.defaults |= Notification.DEFAULT_VIBRATE;
        }

        if (soundUri != null) {
            Log.v("NotificationEasier: phone playing ringtone");
            notification.sound = Uri.parse(soundUri);
        }

        // Flashing light
        notification.defaults |= Notification.DEFAULT_LIGHTS;
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

        return notification;
    }
}
